package util;

import org.apache.commons.mail.HtmlEmail;

public class SendEmailUtil {

    public static boolean sendEmail(String account, String code) {
        try {
            //创建网页邮箱对象
            HtmlEmail email = new HtmlEmail();

            //基本设置
            email.setDebug(false);

            email.setSSLOnConnect(false);

            //设置为QQ邮箱作为发送主邮箱
            email.setHostName("SMTP.qq.com");
            email.setSmtpPort(587);

            //qq邮箱的验证信息
            email.setAuthentication("2394412110@qq.com", "yfwwrellfqslebjc");

            //设置邮件发送人
            email.setFrom("2394412110@qq.com");

            //设置邮件接收人
            email.addTo(account);

            //设置发送的内容
            email.setMsg("【STA-BLOG】如非本人操作，请忽略！\n\\`v'/\n您的验证码是" + code + "五分钟内有效，请不要把验证码泄露给其他人哦\n");

            //设置邮箱标题
            email.setSubject("This is your captcha.");

            //执行邮件发送
            email.send();
            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        VerifiCode c = new VerifiCode();
        String code = c.getC();
        boolean b = sendEmail("2394412110@qq.com", code);
//        boolean b=true;
//        1158965969@qq.com 天天的邮箱
        //843497509@qq.com hyx的邮箱
        if (b) {
            System.out.println("发送成功");
        } else {
            System.out.println("发送失败");
        }
    }
}
